﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using PBDesk.Utilities;
using System.Web;
using Umbraco.Framework.Dynamics;
using System.Web.Helpers;
using PBDesk.com.Extension.WebHelper;
using System.Globalization;
using Umbraco.Cms.Web;
using PBDesk.com.Extension.FeedsAggregator;



namespace PBDesk.com.Extension.UmbHelper
{
    public class MyHelper
    {
        private const string DEFAULT_THEME = "Rambling";
        public static string GetTheme()
        {
            string theme = DEFAULT_THEME;
            var themeEnabled = ConfigurationManager.AppSettings["ThemeEnabled"] ?? "false";
            if (themeEnabled.ToLower() == "true")
            {
                theme = HttpContext.Current.Request.QueryString["t"];
                if (theme.IsNullOrEmpty())
                {
                    theme = ConfigurationManager.AppSettings["Theme"] ?? DEFAULT_THEME;
                }
                if (theme.IsNullOrEmpty())
                {
                    theme = DEFAULT_THEME;
                }
            }
            return theme;
        }
        public static string GetBannerColumnStyle(int colCount)
        {
            return (colCount == 1) ? "columns100" : (colCount == 2 ? "columns5050" : "columns");
        }

        public static string GetCanonicalUrl(string url)
        {
            var canonicalUrl = String.Empty;
            if (HttpContext.Current != null)
            {
                string host = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
                if (host.ToLower().StartsWith("www"))
                {
                    canonicalUrl = string.Concat("http://", host, url);
                }
                else
                { canonicalUrl = string.Concat("http://www.", host, url); }
            }
            return canonicalUrl;
        }
        public static string GetCanonicalUrl(BendyObject dynaModel)
        {
            return GetCanonicalUrl(dynaModel.AsDynamic().Url);
        }

        public static BendyObject GetArticleOfTheDay(BendyObject dynaModel, string previewDate = "")
        {
            DateTime previewDateDt = DateTime.Today;
            BendyObject artOfDay = null;
            if (previewDate.IsNotNullAndNotEmpty() && previewDate.ToLower() != "today")
            {
                DateTime tempDt;
                if (DateTime.TryParseExact(previewDate, "dd/MM/yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out tempDt))
                {
                    previewDateDt = tempDt;
                }
            }

            if (previewDateDt == DateTime.Today)
            {
                artOfDay = CacheManager.GetArticleOfTheDay();
                if (artOfDay != null)
                    return artOfDay;
            }

            artOfDay = GetHomeNode(dynaModel).AsDynamic().Descendants.FirstOrDefault("PreviewDate == @0", previewDateDt.ToString("MM/dd/yyyy"));
            CacheManager.SetArticleOfTheDay(artOfDay);
            return artOfDay;
        }

        public static BendyObject GetHomeNode(BendyObject dynaModel)
        {            
            var home = CacheManager.GetHomeNode();
            if (home == null)
            {
                home = dynaModel.AsDynamic().AncestorsOrSelf.First("NodeTypeAlias == @0", "HomePage");
                CacheManager.SetHomeNode(home);                                
            }
            return home;

        }

        public static C9Item GetTWC9()
        {
            var c9Item = CacheManager.GetTWC9Item();
            if (c9Item == null)             
            {
                c9Item = FeedHelper.GetTWC9();
                CacheManager.SetTWC9Item(c9Item);
                
            }
            return c9Item;
        }

        public static List<C9Item> GetTWC9Items(int itemCount = 50)
        {
            return FeedHelper.GetTWC9Items(itemCount);
        }

       
       

        
    }
}
